define(['jquery', 'fast'], function ($, Fast) {
    var District = {
        data: {
            api: {
                provinceList: 'district/district_list',
                cityList: 'district/district_list',
                districtList: 'district/district_list',
                streetList: 'district/district_list',
            },
            data: {},
            defaultData: {
                provicneTitle: "请选择省",
                cityTitle: "请选择市",
                districtTitle: "请选择区",
                streetTitle: "请选择街道",
            }
        },
        // 国省市区街道四级联动处理函数
        districtCpcdsEvent: function (options = {}) {
            var self = this;
            var type = options.type ? options.type : 2;
            var class_name = options.class_name ? options.class_name : 'district';

            // 初始化
            this.initAddress(type, class_name);
            // 省变化
            let countryDiv = "."+class_name+" select.country";
            let provinceDiv = "."+class_name+" select.province";
            let cityDiv = "."+class_name+" select.city";
            let districtDiv = "."+class_name+" select.district";
            let streetDiv = "."+class_name+" select.street";
            $(provinceDiv).change(function (e) {
                self.data.data.provinceId = $(this).val();
                self.data.data.cityId = 0;
                self.data.data.districtId = 0;
                self.data.data.streetId = 0;
                self.getCityList($(this).val());
            });
            // 市变化
            $(cityDiv).change(function (e) {
                self.data.data.cityId = $(this).val();
                self.data.data.districtId = 0;
                self.data.data.streetId = 0;
                self.getDistrictList($(this).val());
            });
            // 区变化
            $(districtDiv).change(function (e) {
                self.data.data.districtId = $(this).val();
                self.data.data.streetId = 0;
                self.getStreetList($(this).val());
            });
            // 街道变化
            $(streetDiv).change(function (e) {
                self.data.data.streetId = $(this).val();
            });
        },
        initAddress: function (type, class_name) {
            var self = this;
            var countryDiv = "."+class_name+" select.country";
            var provinceDiv = "."+class_name+" select.province";
            var cityDiv = "."+class_name+" select.city";
            var districtDiv = "."+class_name+" select.district";
            var streetDiv = "."+class_name+" select.street";

            var countryId = $(countryDiv).data('id');
            var provinceId = $(provinceDiv).data('id');
            var cityId = $(cityDiv).data('id');
            var districtId = $(districtDiv).data('id');
            var streetId = $(streetDiv).data('id');
            self.data.data = {
                countryId: countryId,
                provinceId: provinceId,
                cityId: cityId,
                districtId: districtId,
                streetId: streetId,
                countryDiv: countryDiv,
                provinceDiv: provinceDiv,
                cityDiv: cityDiv,
                districtDiv: districtDiv,
                streetDiv: streetDiv,
                type: type,
            };
            
            self.getProvinceList(1);
        },
        // 获取省列表
        getProvinceList: function (countryId) {
            var self = this;
            
            // 获取省数据
            Fast.api.ajax({
                url: this.data.api.provinceList+"?type="+this.data.data.type,
                method: 'POST',
                data: {
                    parent_id: countryId,
                    level: 'province'
                }
            }, function (provinceList) {
                var provinceString = self.createOptionSplicing(provinceList, self.data.data.provinceId, {title: self.data.defaultData.provicneTitle});
                var provinceId = 0;
                if (provinceList.length > 0) {
                    provinceId = self.data.data.provinceId > 0 ? self.data.data.provinceId : 0
                }
                $(self.data.data.provinceDiv).html(provinceString);
                
                // 清空其它三个联动Dom结构
                var cityString = self.createEmptyOptions({title: self.data.defaultData.cityTitle}, false);
                $(self.data.data.cityDiv).html(cityString);

                var districtString = self.createEmptyOptions({title: self.data.defaultData.districtTitle}, false);
                $(self.data.data.districtDiv).html(districtString);

                var streetString = self.createEmptyOptions({title: self.data.defaultData.streetTitle}, false);
                $(self.data.data.streetDiv).html(streetString);
                if (provinceId > 0) {
                    self.getCityList(provinceId);
                }

                return false;
            });
        },
        // 市列表
        getCityList: function (provinceId) {
            var self = this;

            // 获取市数据
            Fast.api.ajax({
                url: this.data.api.cityList+"?type="+this.data.data.type,
                method: 'POST',
                data: {
                    parent_id: provinceId,
                    level: 'city'
                }
            }, function (cityList) {
                var cityString = self.createOptionSplicing(cityList, self.data.data.cityId, {title: self.data.defaultData.cityTitle});
                var cityId = 0;
                if (cityList.length > 0) {
                    cityId = self.data.data.cityId > 0 ? self.data.data.cityId : 0
                }
                $(self.data.data.cityDiv).html(cityString);
                
                // 清空其它二个联动Dom结构
                var districtString = self.createEmptyOptions({title: self.data.defaultData.districtTitle}, false);
                $(self.data.data.districtDiv).html(districtString);

                var streetString = self.createEmptyOptions({title: self.data.defaultData.streetTitle}, false);
                $(self.data.data.streetDiv).html(streetString);
                
                if (cityId > 0) {
                    self.getDistrictList(cityId);
                }

                return false;
            });
        },
        // 区列表
        getDistrictList: function (cityId) {
            var self = this;
            
            // 获取区数据
            Fast.api.ajax({
                url: this.data.api.districtList+"?type="+this.data.data.type,
                method: 'POST',
                data: {
                    parent_id: cityId,
                    level: 'district'
                }
            }, function (districtList) {
                var districtString = self.createOptionSplicing(districtList, self.data.data.districtId, {title: self.data.defaultData.districtTitle});
                
                var districtId = 0;
                if (districtList.length > 0) {
                    districtId = self.data.data.districtId > 0 ? self.data.data.districtId : 0
                }
                $(self.data.data.districtDiv).html(districtString);
                
                // 清空其它一个联动Dom结构
                var streetString = self.createEmptyOptions({title: self.data.defaultData.streetTitle}, false);
                $(self.data.data.streetDiv).html(streetString);

                if (districtId > 0) {
                    self.getStreetList(districtId);
                }

                return false;
            });
        },

        // 街道列表
        getStreetList: function (districtId) {
            var self = this;
            
            // 获取区数据
            Fast.api.ajax({
                url: this.data.api.streetList+"?type="+this.data.data.type,
                method: 'POST',
                data: {
                    parent_id: districtId,
                    level: 'street'
                }
            }, function (streetList) {
                var streetString = self.createOptionSplicing(streetList, self.data.data.streetId, {title: self.data.defaultData.streetTitle});
                $(self.data.data.streetDiv).html(streetString);
                
                return false;
            });
        },

        createOptionSplicing: function (list, selectedId = 0, defaultOptions = {}) {
            var seletedFlag = false;
            if (selectedId <= 0) {
                seletedFlag = true;
            }
            
            var string =this.createEmptyOptions(defaultOptions, seletedFlag);
            for (let i = 0; i < list.length; i++) {
                var seletedStr = '';
                
                if (selectedId > 0 && list[i].id == selectedId) {
                    seletedStr = ' selected="true"';
                }
                string += '<option value="' + list[i].id + '"' + seletedStr + '>' + list[i].name + '</option>';
            }

            return string;
        },

        // 创建空选项
        createEmptyOptions: function (defaultOptions = {}, selected = false) {
            var seletedStr = '';
            if (selected <= 0) {
                seletedStr = ' selected="true"';
            }

            var string = '<option value="0"' + seletedStr + '>' + defaultOptions.title + '</option>';

            return string;
        }
    };

    return District;
});